import { brands } from '../util/brand'
import moment from 'moment'

export default {
  data() {
    return {
      months: [],
      seasons: ['春', '夏', '秋', '冬'],
      openYear: false,
      year: null,
      typeList: [
        {
          name: '有搭素材',
          code: 'B10-A01'
        },
        {
          name: '零售素材',
          code: 'B10-A02'
        },
        {
          name: '导购素材',
          code: 'B10-A03'
        },
        {
          name: '微商城',
          code: 'B10-A04'
        }
      ],
      dataSource: [],
      brands,
      form: this.$form.createForm(this),
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      },
      selectedRowKeys: []
    }
  },
  watch:{
    $route:{
      handler(to, from){
        if(!from){
          this.type == 2
          this.resetForm()
          return
        }
        console.log(to.path,from.path)
        if(to.path == '/micromall/marketing/collocation/official'){
          // 官方搭配 新建编辑 刷新当前页
          console.log('官方搭配')
          if(from.path == '/micromall/marketing/collocation/edit' || from.path == '/micromall/marketing/collocation/create'){
            this.loadData()
          }else{
            this.type == 2
            this.resetForm()
          }
        }
        if(to.path == '/micromall/marketing/collocation/more'){
          // 更多搭配 新建编辑 刷新当前页
          console.log('更多搭配')
          if(from.path == '/micromall/marketing/collocation/more/create' || from.path == '/micromall/marketing/collocation/more/edit'){
            this.loadData()
          }else{
            this.type == 2
            this.resetForm()
          }
        }
      },
      immediate: true
    }
  },
  computed: {
    filterLable() {
      return function (codes) {
        return this.styleOptions.filter(item => codes.includes(item.code)).map(item => item.name).join(',')
      }
    }
  },
  activated() {
    this.clearKeys()
    this.brands = brands.filter(item => item.weid != 5 && item.weid != 11)
    this.months = Array.from({ length: 12 }, (_, i) => 1 + (i))
    this.styleList()
  },
  methods: {
    clearKeys(){
      this.selectedRowKeys = []
    },
    getQueryParams() {
      const params = this.form.getFieldsValue()
      const { current, pageSize: size } = this.pagination
      return {
        ...params,
        current,
        size
      }
    },
    handleSubmit(e) {
      e.preventDefault()
      this.pagination.current = 1
      this.loadData()
    },
    resetForm() {
      console.log('重置表达')
      this.form.resetFields()
      this.loadData(1)
    },
    handleTableChange(pagination) {
      console.log('gaibian', pagination)
      this.pagination = pagination
      this.loadData()
    },
    reCalculatePage(count) {
      //总数量-count
      let total = this.pagination.total - count;
      //获取删除后的分页数
      let currentIndex = Math.ceil(total / this.pagination.pageSize);
      //删除后的分页数<所在当前页
      if (currentIndex < this.pagination.current) {
        this.pagination.current = currentIndex;
      }
      console.log('currentIndex', currentIndex)
    },
    openChange(status) {
      this.openYear = status
    },
    handleChange(value) {
      console.log(value)
      this.year = value;
    },
    panelChange(value, mode) {
      console.log('panelChange', value)
      this.year = moment(value).format('YYYY')
      this.openYear = false;
    },

    resetForm() {
      console.log('重置表达')
      this.year = null
      this.form.resetFields()
      this.loadData(1)
    },
    getQueryParams() {
      const params = this.form.getFieldsValue()
      if (this.year) {
        params.year = this.year
      }
      if (params.time) {
        params.startTime = moment(params.time[0]).format('YYYY-MM-DD')
        params.endTime = moment(params.time[1]).format('YYYY-MM-DD')
        delete params.time
      }
      const { current, pageSize: size } = this.pagination
      return {
        ...params,
        current,
        size
      }
    },
  },
}